import 'dart:async';

import 'package:get/get.dart';

class VerificationCodeController extends GetxController {
  /// 响应式成员变量
  final _secondsRemaining = 60.obs;

  set secondsRemaining(value) => _secondsRemaining.value = value;

  get secondsRemaining => _secondsRemaining.value;

  /// 成员变量
  late Timer? _timer;

  /// 事件
  void startCountdown() {
    secondsRemaining = 60;
    _timer = Timer.periodic(const Duration(seconds: 1), (timer) {
      if (secondsRemaining > 0) {
        secondsRemaining--;
      } else {
        timer.cancel();
      }
    });
  }

  void stopCountdown() {
    _timer?.cancel();
  }

  /// 生命周期
  @override
  void dispose() {
    super.dispose();
    stopCountdown();
  }
}
